package com.sky.aspect;

import com.sky.annotation.AutoFill;
import com.sky.constant.AutoFillConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.time.LocalDateTime;

@Aspect
@Component
@Slf4j
public class AutoFillAspect {

    //切入點，即哪些地方要執行自動填充
    @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)")
    public void autoFillPointCut() {
    }

    //前置通知,在原來的方法運行前，先執行以下代碼
    @Before("autoFillPointCut()")
    public void autoFill(JoinPoint joinPoint) {
        log.info("開始自動填寫");
        //獲取簽名對象,例如:com.sky.mapper.EmployeeMapper.save(Employee employee)
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //獲取簽名對象的方法(save)的上面注解
        AutoFill annotation = signature.getMethod().getAnnotation(AutoFill.class);
        //獲取操作類型
        OperationType operationType = annotation.value();


        Object[] args = joinPoint.getArgs();
        if (args == null || args.length == 0) {
            return;
        }
        //獲取操作實體對象(例如:Employee)
        Object entity = args[0];
        //準備填寫時間和操作人
        LocalDateTime now = LocalDateTime.now();
        //從當前綫程中獲取操作人id
        Long currentId = BaseContext.getCurrentId();

        //通過反射，將時間和操作人填入實體對象
        if (operationType == OperationType.INSERT) {
            try {
                //獲取實體對象的createTime、updateTime、createUser、updateUser方法
                Method createTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_TIME, LocalDateTime.class);
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method createUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_CREATE_USER, Long.class);
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                //通過反射，將時間和操作人填入實體對象
                createTime.invoke(entity, now);
                updateTime.invoke(entity, now);
                createUser.invoke(entity, currentId);
                updateUser.invoke(entity, currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (operationType == OperationType.UPDATE) {
            try {
                Method updateTime = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_TIME, LocalDateTime.class);
                Method updateUser = entity.getClass().getDeclaredMethod(AutoFillConstant.SET_UPDATE_USER, Long.class);

                updateTime.invoke(entity, now);
                updateUser.invoke(entity, currentId);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
